package cn.tedu.controller;


import cn.tedu.config.AlipayConfig;
import cn.tedu.service.PayService;
import com.alipay.api.internal.util.AlipaySignature;
import com.jt.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Controller
@RequestMapping("/pay/manage")
public class PayController {
    @Autowired
    private PayService payService;
    /**
     *
     * @Title: AlipayController.java
     * @Description: 前往支付宝第三方网关进行支付
     */
    @RequestMapping(value = "/goAlipay", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String goAlipay(String orderId, String orderName,Double price,
                           HttpServletRequest request, HttpServletRequest response) throws Exception {
        return payService.goPay(orderId);
    }
    @RequestMapping("/paySuccess")
    @ResponseBody
    public String paySuccess(HttpServletRequest request, HttpServletRequest response) throws Exception {
        // 获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset,
                AlipayConfig.sign_type); // 调用SDK验证签名

        //ModelAndView mv = new ModelAndView("alipaySuccess");
        // ——请在这里编写您的程序（以下代码仅作参考）——
        if (signVerified) {
            //商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 付款金额
            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
            try {
                payService.updateOrderStatus(out_trade_no, trade_no, total_amount);
                return "<a href='http://www.easymall.com'>您已成功支付，请返回主页查看订单</a>";
            } catch (Exception e) {
                e.printStackTrace();
                return "支付失败";
            }
        }
        return "支付失败";
    }


}
